class Solution:
    def maxScore(self, cardPoints: list, k: int) -> int:
        left = 0
        right = len(cardPoints) - 1 - k
        sumVal = 0
        for i in range(right + 1):
            sumVal += cardPoints[i]
        ans = 2**31
        while True:
            ans = min(ans, sumVal)
            right += 1
            if right >= len(cardPoints):
                break
            sumVal += cardPoints[right]
            sumVal -= cardPoints[left]
            left += 1

        return sum(cardPoints) - ans
